/*
 *	WebSQL Example
 *	관련파일: web_sql.html
*/

function $(id)
{
	return document.getElementById(id);
}

function Init()
{
	var db;
	var dbName = 'school';
	var dbVer = 1.0
	var dbComment = '성적처리';
	var dbSize = 5 * 1024 * 1024; //5mb
	$('createDB').onclick = function()
	{
		db = openDatabase(dbName, dbVer, dbComment, dbSize);
		console.log(dbName+' DB가 생성됨.');
	};
	$('dropDB').onclick = function()
	{
		db.transaction
		(
			function(tx)
			{
				var sql='drop database' + dbName;
				tx.executeSql(sql);
				console.log(dbName+' DB가 삭제됨.');
			}
		);
	};
	$('createTable').onclick = function()
	{
		db.transaction
		(
			function(tx)
			{
				var sql = 'create table if not exists score (mid integer, irum text, kor integer, eng integer, tot integer, avg integer)';
				tx.executeSql(sql);
				console.log('member 테이블이 생성됨...');
			}
		);
	};
	$('dropTable').onclick = function()
	{
		if(db != null)
		{
			db.transaction
			(
				function(tx)
				{
					var sql='drop table score';
					tx.executeSql(sql);
					console.log('테이블이 삭제됨');
				}
			);
		}
	};
	
	$('insert').onclick = function()
	{
		db.transaction
		(
			function(tx)
			{
				var mid = $('mid').value;
				var irum = $('irum').value;
				var kor = Number($('kor').value);
				var eng = Number($('eng').value);
				var tot = kor + eng;
				var avg = tot/2;
				var sql = 'insert into score(mid, irum, kor, eng, tot, avg) values(?, ?, ?, ? , ? ,?)';
				tx.executeSql(sql, [mid, irum, kor, eng, tot, avg]);
				
				console.log('데이터가 추가됨');
			}
		);
	};
	$('update').onclick = function()
	{
		db.transaction
		(
			function(tx)
			{
				var mid = $('mid').value;
				var irum = $('irum').value;
				var sql = 'update member set irum = ? where mid=?';
				tx.executeSql
				(
					sql, [irum, mid], function(){console.log(mid + '정보가 수정됨');}, function(){alert('수정중 오류발생');}
				);
			}
		);
	};
	$('delete').onclick = function()
	{
		db.transaction
		(
			function(tx)
			{
				var mid = $('mid').value;
				var sql = 'delete from member where mid=?';
				tx.executeSql
				(
					sql, [ mid], function(){console.log(mid + '정보가 삭제됨');}, function(){alert('삭제중 오류발생');}
				);
			}
		)
	};
	$('select').onclick = function()
	{
		db.transaction
		(
			function(tx)
			{
				var sql = 'select * from score';
				var str = '';
				tx.executeSql
				(
					sql, [], function(tx, rs)
					{
						for(i=0; i<rs.rows.length; i++)
						{
							var row = rs.rows.item(i);
							str +=  ' 아이디 : ' + row.mid + ' | 이름 : ' + row.irum
                                    + ' 국어 : ' + row.kor
                                    + '점 | 영어 : ' + row.eng + '점'
                                    + ' 총점 : ' + row.tot + '점 | 평균 : '
                                    + row.avg + '점' + '<br/>';
						}
						$('result').innerHTML = str;
						console.log('데이터 조회');
					}
				);
			}
		);
	};
	$("graph").onclick=function(){
        db.transaction(
             function(tx){
                  var str = "";
                  var mid = $("mid").value;
                  ctx = $("canvas").getContext("2d");
                  ctx.clearRect(0, 0, canvas.width, canvas.height)
                  var sql = "select * from score where mid =? ";
                  tx.executeSql(sql, [mid], function(tx,rs){
                       for ( i = 0; i < rs.rows.length; i++) {
                            var row = rs.rows.item(i);

                                 var kor = row.kor;
                                 var eng= row.eng;
                                 var tot = row.tot;
                                 var avg = row.avg;                              

                                 ctx.strokeText('국어', 50, 220);
                                 ctx.strokeText('영어', 100,220);
                                 ctx.strokeText('총점', 150,220);
                                 ctx.strokeText('평균', 200,220);

                                 ctx.beginPath();
                                 ctx.moveTo(0, 200);
                                 ctx.lineTo(300, 200);
                                 ctx.closePath();
                                 ctx.stroke();
				
                                 ctx.beginPath();
                                 ctx.moveTo(50 + (i * 10), 200);
                                 ctx.lineTo(50 + (i * 10), 200 - kor);
                                 ctx.strokeText(kor, 40+(i*10),200-(i*10)-kor);
                                 ctx.closePath();
                                 ctx.stroke();
				
                                 ctx.beginPath();
                                 ctx.moveTo(100 + (i * 10), 200);
                                 ctx.lineTo(100 + (i * 10), 200 - eng);
                                 ctx.strokeText(eng, 90+(i*10),200-(i*10)-eng);
                                 ctx.closePath();
                                 ctx.stroke();
                                 
                                 ctx.beginPath();
                                 ctx.moveTo(150 + (i * 10), 200);
                                 ctx.lineTo(150 + (i * 10), 200 - tot);
                                 ctx.strokeText(tot, 140+(i*10),200-(i*10)-tot);
                                 ctx.closePath();
                                 ctx.stroke();
                                 
                                 ctx.beginPath();
                                 ctx.moveTo(200 + (i * 10), 200);
                                 ctx.lineTo(200 + (i * 10), 200 - avg);
                                 ctx.strokeText(avg, 190+(i*10),200-(i*10)-avg);
                                 ctx.closePath();
                                 ctx.stroke();

                       }
                  })
                  console.log(dbName+"Data가 조회됨");
             }
        );
   }
}
